//
//  YIRegister.m
//  一点互助
//
//  Created by rimi on 16/8/20.
//  Copyright © 2016年 shen. All rights reserved.
//

#import "YIRegister.h"
#import "YICustomField.h"
@interface YIRegister ()
{
       UIButton *sendNumber;
       UIButton *registerBtn;
       UITextField *phoneNumber;
       UITextField *verification;
}
@end

@implementation YIRegister

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1];
             [self headView];
       [self referrer];
       [self phoneNumberTextField];
       [self passWordTextField];
       [self verification];
       [self registerBtn];
}
#pragma mark --layout
-(void)headView{
       UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kBaseWidth, 64)];
       headView.backgroundColor = HEADER_COLOR;
       [self.view addSubview:headView];
       
       UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
             [headView addSubview:backBtn];
       backBtn.frame = CGRectMake(15, 30 , 40, 20);
       [backBtn setImage:[UIImage imageNamed:@"个人中心-left"] forState:UIControlStateNormal];
       [backBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
       [headView addSubview:backBtn];
       
       UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth / 2 - 30, 30, 60, 20)];
       titleLable.text = @"注册";
       titleLable.textColor = [UIColor whiteColor];
       titleLable.font = [UIFont systemFontOfSize:15];
       [headView addSubview:titleLable];
       
       
}

//推荐人
-(void)referrer{
       UILabel *referrer = [[UILabel alloc]initWithFrame:CGRectMake(15, 90, 60, 30)];
       referrer.font = [UIFont systemFontOfSize:13];
       referrer.text = @"推荐人";
       [self.view addSubview:referrer];
       
       YICustomField *textField = [YICustomField new];
       textField.frame = CGRectMake(60, 93, 80, 20);
       textField.backgroundColor = [UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1];
       textField.textAlignment = NSTextAlignmentCenter;
       textField.font = [UIFont systemFontOfSize:15];
       [self.view addSubview:textField];
       UILabel *rightLable = [[UILabel alloc]initWithFrame:CGRectMake(140, 90, 60, 30)];
       rightLable.text = @"(选填)";
       rightLable.font = [UIFont systemFontOfSize:13];
       [self.view addSubview:rightLable];
}
//电话号码
-(void)phoneNumberTextField{
       UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 120, kScreenWidth, 50)];
       backView.backgroundColor = [UIColor whiteColor];
       [self.view addSubview:backView];
       
       UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 30, 30)];
       leftImage.image = [UIImage imageNamed:@"登录注册-手机"];
       [backView addSubview:leftImage];
       
       phoneNumber = [[UITextField alloc]initWithFrame:CGRectMake(70, 10, kScreenWidth - 80, 30)];
       phoneNumber.tag = 100;
       phoneNumber.placeholder = @"请输入手机号";
       phoneNumber.font = [UIFont systemFontOfSize:15];
       phoneNumber.backgroundColor = [UIColor whiteColor];
       phoneNumber.clearButtonMode = UITextFieldViewModeAlways;
       //监听事件
       [phoneNumber addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
       [backView addSubview:phoneNumber];
       
       UILabel *phoneNumberLeft = [[UILabel alloc]initWithFrame:CGRectMake(60, 18, 1, 13)];
       phoneNumberLeft.backgroundColor = [UIColor grayColor];
       [backView addSubview:phoneNumberLeft];
       
       sendNumber = [UIButton buttonWithType:UIButtonTypeCustom];
       sendNumber.frame = CGRectMake(kScreenWidth - 120, 5, 100, 40);
       [sendNumber setTitle:@"获取验证码" forState:0];
       sendNumber.layer.cornerRadius = 10;
       sendNumber.titleLabel.font = [UIFont systemFontOfSize:17];
       [sendNumber setTitleColor:[UIColor colorWithRed:0.6353 green:0.6353 blue:0.6353 alpha:1.0] forState:0];
       sendNumber.tag = 103;
       [sendNumber.layer setBorderWidth:2];
       [sendNumber.layer setBorderColor:[UIColor colorWithRed:0.7922 green:0.7922 blue:0.7922 alpha:1.0].CGColor];
       [backView addSubview:sendNumber];
}
//密码
-(void)passWordTextField{
       UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 172, kScreenWidth, 50)];
       backView.backgroundColor = [UIColor whiteColor];
       [self.view addSubview:backView];
       
       UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 30, 30)];
       leftImage.image = [UIImage imageNamed:@"登录注册-钥匙"];
       [backView addSubview:leftImage];
       
       UITextField *passWord = [[UITextField alloc]initWithFrame:CGRectMake(70, 10, kScreenWidth - 80, 30)];
       passWord.placeholder = @"请输入密码";
       passWord.secureTextEntry = YES;
       passWord.font = [UIFont systemFontOfSize:15];
       passWord.backgroundColor = [UIColor whiteColor];
       passWord.clearButtonMode = UITextFieldViewModeAlways;
       [backView addSubview:passWord];
       
       UILabel *passWordLeft = [[UILabel alloc]initWithFrame:CGRectMake(60, 18, 1, 13)];
       passWordLeft.backgroundColor = [UIColor grayColor];
       [backView addSubview:passWordLeft];

}
//验证码
-(void)verification{
       UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 224, kScreenWidth, 50)];
       backView.backgroundColor = [UIColor whiteColor];
       [self.view addSubview:backView];
       
       UIImageView *leftImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 30, 30)];
       leftImage.image = [UIImage imageNamed:@"登录注册-验证码"];
       [backView addSubview:leftImage];
       
       verification = [[UITextField alloc]initWithFrame:CGRectMake(70, 10, kScreenWidth - 80, 30)];
       verification.tag = 101;
       verification.placeholder = @"请输入验证码";
       verification.font = [UIFont systemFontOfSize:15];
       verification.backgroundColor = [UIColor whiteColor];
       verification.clearButtonMode = UITextFieldViewModeAlways;
       [backView addSubview:verification];
       //添加监听事件
       [verification addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
       UILabel *verificationLeft = [[UILabel alloc]initWithFrame:CGRectMake(60, 18, 1, 13)];
       verificationLeft.backgroundColor = [UIColor grayColor];
       [backView addSubview:verificationLeft];
}
//注册按钮
-(void)registerBtn{
       registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
       registerBtn.frame = CGRectMake(kScreenWidth / 2 - (kScreenWidth - 80)/2, 300, kScreenWidth - 80, 50);
       registerBtn.layer.cornerRadius = 5;
       registerBtn.backgroundColor = [UIColor colorWithRed:0.6039 green:0.6039 blue:0.6039 alpha:1.0];
       [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
       registerBtn.titleLabel.font = [UIFont systemFontOfSize:15];
       [registerBtn setTintColor:[UIColor whiteColor]];
       registerBtn.tag = 104;
       [self.view addSubview:registerBtn];
       
}

#pragma mark --click

//返回
-(void)click{
       [self dismissViewControllerAnimated:YES completion:nil];
}

//设置textField的监听事件,当输入时的监听事件
- (void) textFieldDidChange:(UITextField *) TextField{
       if (TextField.tag == 101) {
              if (TextField.text.length > 0) {
                     registerBtn.backgroundColor = HEADER_COLOR;
                     [registerBtn addTarget:self action:@selector(gainClick:) forControlEvents:UIControlEventTouchUpInside];
              }
              
              //验证码框的输入内容长度为为0的时候改变按钮颜色
              if (TextField.text.length <= 0 ) {
                     registerBtn.backgroundColor = [UIColor colorWithRed:0.6039 green:0.6039 blue:0.6039 alpha:1.0];
                     
              }
       }
       if (TextField.tag == 100) {
              if (TextField.text.length > 0) {
                     [sendNumber.layer setBorderColor:[UIColor colorWithRed:0.2902 green:0.7647 blue:0.7294 alpha:1.0].CGColor];
                     [sendNumber setTitleColor:[UIColor colorWithRed:0.2902 green:0.7647 blue:0.7294 alpha:1.0] forState:0];
                     [sendNumber addTarget:self action:@selector(gainClick:) forControlEvents:UIControlEventTouchUpInside];
              }
              
              if (TextField.text.length <= 0) {
                     [sendNumber.layer setBorderColor:[UIColor colorWithRed:0.7922 green:0.7922 blue:0.7922 alpha:1.0].CGColor];
                     [sendNumber setTitleColor:[UIColor colorWithRed:0.6353 green:0.6353 blue:0.6353 alpha:1.0] forState:0];
              }
              
       }
       
}
-(void)nilClick{
}
-(void)gainClick:(UIButton *)btn{
       if (btn.tag == 103) {
              if (phoneNumber.text.length > 0) {
                      PLLOG(@"获取验证码");
              }
             
       }
       if (btn.tag == 104) {
              if (verification.text.length > 0) {
                     PLLOG(@"注册");
              }
       }
}
@end
